package com.vhr.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vhr.VO.SalaryCountVO;
import com.vhr.VO.SalaryVO;
import com.vhr.entities.*;
import com.vhr.mapper.SalaryMapper;
import com.vhr.service.ILeaveService;
import com.vhr.service.ISalaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@Api(description = "薪资模块")
@CrossOrigin
public class SalaryController {

    @Autowired
    private ISalaryService iSalaryService;

    @Autowired
    private ILeaveService iLeaveService;

    @Autowired
    private SalaryMapper salaryMapper;

    /**查询单个员工不同月的所有的工资selectAllEmpSalary
    访问：http://localhost:8080/salary/selectOneEmpSalary*/
    @ApiOperation("用户：查询单个员工不同月的所有的工资")
    @CrossOrigin//跨域请求操作
    /*
    @RequestMapping(value = "/salary/selectOneEmpSalary",method = RequestMethod.GET)

    访问：http://localhost:8080/salary/selectOneEmpSalary/{id}
    public Result selectOneEmpSalary(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "2")Integer pageSize,
                                                 @PathVariable("id")Integer id){*/
    @GetMapping("/salary/selectOneEmpSalary")
    public Result selectOneEmpSalary(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                                     @RequestParam("id")Integer id){

        Employee employee = new Employee();
        employee.setEmpId(id);

        SalaryExample salaryExample = new SalaryExample();
        SalaryExample.Criteria criteria = salaryExample.createCriteria();
        criteria.andEmpIdEqualTo(employee.getEmpId());

        PageHelper.startPage(pageNum,pageSize);
        List<Salary> list = salaryMapper.selectByExample(salaryExample);
        /*System.out.println(list.size()+"-------------Salary-list------------------");
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i).toString()+"list(i)------------------");
        }
        System.out.println(list.toString()+"-------------list.toString-------------");*/
        PageInfo<Salary> pageInfo = new PageInfo<>(list);
        if(pageInfo.getList()==null){
            return Result.fail();
        }
        ArrayList<SalaryToPage> salaryVOList = new ArrayList<>();
        Salary salary = new Salary();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

        List<SalaryToPage> salaryToPages = iSalaryService.selectOneEmpSalary(employee);
        /*System.out.println("+++++++++salaryToPages.size()++++++++++"+salaryToPages.size()+"++++++++++++++++++++");
        for(int k=0;k<salaryToPages.size();k++){
            System.out.println("---"+salaryToPages.get(k).toString());
        }*/

        for(int i=0;i<list.size();i++){

            Salary s = list.get(i);
            //System.out.println("/////////////////////getSalaryforList"+s.toString()+"----------------------");

            SalaryToPage vo = new SalaryToPage();

            for(int n=0;n<salaryToPages.size();n++){
                if(list.get(i).getSalaryId()==salaryToPages.get(n).getId()){
                    vo.setId(salaryToPages.get(n).getId());
                    vo.setBasic(salaryToPages.get(n).getBasic());
                    vo.setDate(salaryToPages.get(n).getDate());
                    vo.setReal_salary(salaryToPages.get(n).getReal_salary());
                    vo.setTex(salaryToPages.get(n).getTex());
                    vo.setLeave(salaryToPages.get(n).getLeave());
                    vo.setCheck(salaryToPages.get(n).getCheck());
                    vo.setHouse(salaryToPages.get(n).getHouse());
                    vo.setTravel(salaryToPages.get(n).getTravel());
                    vo.setFood(salaryToPages.get(n).getFood());
                    vo.setOverTime(salaryToPages.get(n).getOverTime());
                    vo.setMonthBonus(salaryToPages.get(n).getMonthBonus());
                    //System.out.println("SalaryEmpVO:-----------"+vo.toString());
                    salaryVOList.add(vo);
                }
            }
        }
        MyPageInfo<SalaryToPage> myPageInfo = new MyPageInfo<>();
        myPageInfo.setList(salaryVOList);
        /*for(int i=0;i<salaryVOList.size();i++){
            System.out.println(salaryVOList.get(i).getOverTime()+"--------salaryVOList--------");
        }*/
        myPageInfo.setPageNum(pageNum);
        myPageInfo.setPageSize(pageSize);
        myPageInfo.setRecordCount((int) pageInfo.getTotal());
        myPageInfo.setTotalPage(pageInfo.getPages());
        return Result.success().add("list",myPageInfo);
    }

    /**查询全部员工不同月的所有的工资
     访问：http://localhost:8080/salary/selectAllEmpSalary*/
    @ApiOperation("管理员：查询全部员工不同月的所有的工资")
    @RequestMapping(value = "/api/salary/selectAllEmpSalary",method = RequestMethod.GET)
    public Result selectAllEmpSalary(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize, Model model){

        PageHelper.startPage(pageNum,pageSize);
        List<Salary> list = iSalaryService.selectAllSalary();

        PageInfo<Salary> pageInfo = new PageInfo<>(list);
        if(pageInfo.getList()==null){
            return Result.fail();
        }
        ArrayList<SalaryToPageAdmin> salaryVOList = new ArrayList<>();
        //Salary salary = new Salary();
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

        List<SalaryToPageAdmin> salaryToPageAdmins = iSalaryService.selectAllEmpSalary();

        for(int i=0;i<list.size();i++){
            Salary s = list.get(i);
            SalaryToPageAdmin vo = new SalaryToPageAdmin();

            for(int n=0;n<salaryToPageAdmins.size();n++){
                if(list.get(i).getSalaryId()==salaryToPageAdmins.get(n).getId()){
                    vo.setId(salaryToPageAdmins.get(n).getId());
                    vo.setTravel(salaryToPageAdmins.get(n).getTravel());
                    vo.setTex(salaryToPageAdmins.get(n).getTex());
                    vo.setReal_salary(salaryToPageAdmins.get(n).getReal_salary());
                    vo.setOverTime(salaryToPageAdmins.get(n).getOverTime());
                    vo.setMonthBonus(salaryToPageAdmins.get(n).getMonthBonus());
                    vo.setHouse(salaryToPageAdmins.get(n).getHouse());
                    vo.setFood(salaryToPageAdmins.get(n).getFood());
                    vo.setDate(salaryToPageAdmins.get(n).getDate());
                    vo.setCheck(salaryToPageAdmins.get(n).getCheck());
                    vo.setBasic(salaryToPageAdmins.get(n).getBasic());
                    vo.setEmpName(salaryToPageAdmins.get(n).getEmpName());
                    vo.setEmpNumber(salaryToPageAdmins.get(n).getEmpNumber());
                    vo.setAward(salaryToPageAdmins.get(n).getAward());

                   // DecimalFormat df = new DecimalFormat("#.00");
                    //System.out.println(Double.parseDouble(df.format(salaryToPageAdmins.get(n).getTakeOut())));
                    vo.setTakeOut(salaryToPageAdmins.get(n).getTakeOut());
                    //vo.setTakeOut(Double.parseDouble(df.format(salaryToPageAdmins.get(n).getTakeOut())));

                    salaryVOList.add(vo);
                }
            }
        }
        MyPageInfo<SalaryToPageAdmin> myPageInfo = new MyPageInfo<>();
        myPageInfo.setList(salaryVOList);
        myPageInfo.setPageNum(pageNum);
        myPageInfo.setPageSize(pageSize);
        myPageInfo.setRecordCount((int) pageInfo.getTotal());
        myPageInfo.setTotalPage(pageInfo.getPages());
        return Result.success().add("list",myPageInfo);
    }

    /*管理员：统计员工薪资信息
    访问：http://localhost:8080/salary/countBySalaryAndDate*/
    @ApiOperation("统计员工薪资信息")
    @GetMapping("/api/salary/countBySalaryAndDate")
    public Result countBySalaryAndDate(@RequestParam(value = "date")Date date){
        System.out.println("日期："+date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Calendar calendar =Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,1);

        String YEAR_MONTH = sdf.format(calendar.getTime());
        Date date1 = null;
        try{
             date1 = sdf.parse(YEAR_MONTH);
        }catch (ParseException e){
            e.printStackTrace();
        }

        List<SalaryCountVO> list = iSalaryService.countBySalary(date1);
        if(list==null){
            return Result.fail();
        }else {
            return Result.success().add("salaryList",list);
        }
    }

    /*管理员：根据条件根据员工名和时间筛选显示部分员工*/
    //访问：http://localhost:8080/salary/selectSomeBodyByNameAndTime
    @ApiOperation("管理员：（方法作废：不支持模糊查询）根据条件根据员工名和时间筛选显示部分员工")
    @GetMapping("/api/salary/selectSomeBodyByNameAndTime")
    public Result selectSomeBodyByNameAndTime(@RequestParam(value = "empId" ,defaultValue = "1") Integer empId,
                                              @RequestParam(value = "payTime",defaultValue = "2019-10") String payTime,
                                              @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                              @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){

        Employee employee = new Employee();
        employee.setEmpId(empId);

        String strdate = payTime+"-12";
        DateFormat datetext = DateFormat.getDateInstance();
        Date date = null;
        try {
            date = datetext.parse(strdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int year = Integer.parseInt(payTime.substring(0,4));
        int month = date.getMonth()+1;
        //System.out.println("-----------------"+year+"--------------"+month);
        Calendar start = Calendar.getInstance();
        start.set(year,month-1,1,0,0,0);
        Calendar end = Calendar.getInstance();
        end.set(year, month,1,0,0,0);

        SalaryExample salaryExample = new SalaryExample();
        SalaryExample.Criteria criteria = salaryExample.createCriteria();
        criteria.andEmpIdEqualTo(employee.getEmpId()).andPayTimeBetween(start.getTime(),end.getTime());

        PageHelper.startPage(pageNum,pageSize);
        List<Salary> list = salaryMapper.selectByExample(salaryExample);

        PageInfo<Salary> pageInfo = new PageInfo<>(list);
        if(pageInfo.getList()==null){
            return Result.fail();
        }
        ArrayList<SalaryToPage> salaryVOList = new ArrayList<>();
        Salary salary = new Salary();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

        List<SalaryToPage> salaryToPages = iSalaryService.selectOneEmpSalary(employee);


        for(int i=0;i<list.size();i++){
            Salary s = list.get(i);
            SalaryToPage vo = new SalaryToPage();

            for(int n=0;n<salaryToPages.size();n++){
                if(list.get(i).getSalaryId()==salaryToPages.get(n).getId()){
                    vo.setId(salaryToPages.get(n).getId());
                    vo.setBasic(salaryToPages.get(n).getBasic());
                    vo.setDate(salaryToPages.get(n).getDate());
                    vo.setReal_salary(salaryToPages.get(n).getReal_salary());
                    vo.setTex(salaryToPages.get(n).getTex());
                    vo.setLeave(salaryToPages.get(n).getLeave());
                    vo.setCheck(salaryToPages.get(n).getCheck());
                    vo.setHouse(salaryToPages.get(n).getHouse());
                    vo.setTravel(salaryToPages.get(n).getTravel());
                    vo.setFood(salaryToPages.get(n).getFood());
                    vo.setOverTime(salaryToPages.get(n).getOverTime());
                    vo.setMonthBonus(salaryToPages.get(n).getMonthBonus());
                    salaryVOList.add(vo);
                }
            }
        }
        MyPageInfo<SalaryToPage> myPageInfo = new MyPageInfo<>();
        myPageInfo.setList(salaryVOList);
        myPageInfo.setPageNum(pageNum);
        myPageInfo.setPageSize(pageSize);
        myPageInfo.setRecordCount((int) pageInfo.getTotal());
        myPageInfo.setTotalPage(pageInfo.getPages());
        return Result.success().add("list",myPageInfo);
    }

    /*管理员：根据条件根据员工名和时间和部门筛选显示部分员工*/
    //访问：http://localhost:8080/salary/selectSomeBodyByNameAndTime
    @ApiOperation("管理员：根据条件根据员工名和时间和部门筛选显示部分员工")
    @GetMapping("/api/salary/selectSomeBodyByNameAndTimeAndDept")
    public Result selectSomeBodyByNameAndTime(@RequestParam(value = "empName" ,defaultValue = "%%") String empName,
                                              @RequestParam(value = "deptId" ,defaultValue = "0")Integer deptId,
                                              @RequestParam(value = "payTime",defaultValue = "%%") String payTime,
                                              @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                              @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        PageHelper.startPage(pageNum,pageSize);
        List<SalaryVO> list = iSalaryService.selectSomeBodyByNameAndTimeAndDept(empName,deptId,payTime);
        PageInfo<SalaryVO> pageInfo = new PageInfo<>(list);
        if(pageInfo.getList()==null){
            return Result.fail();
        }
        ArrayList<SalaryToPageAdmin> salaryVOList = new ArrayList<>();

        for(int i=0;i<list.size();i++){
            SalaryToPageAdmin vo = new SalaryToPageAdmin();

            vo.setId(list.get(i).getSalaryId());
            vo.setTravel(list.get(i).getTravelSalary());
            vo.setTex(list.get(i).getTax());
            vo.setReal_salary(list.get(i).getRealSalary());
            vo.setOverTime(list.get(i).getOvertimeSalary());
            vo.setMonthBonus(list.get(i).getMonthBonus());
            vo.setHouse(list.get(i).getHouseSalary());
            vo.setFood(list.get(i).getFoodSalary());
                String payTime1 = list.get(i).getPayTime().substring(0,7);
            vo.setDate(payTime1);
            vo.setCheck(list.get(i).getIllegalFine()+list.get(i).getLeaveFine());
            vo.setBasic(list.get(i).getBaseSalary());
            vo.setEmpName(list.get(i).getEmpName());
            vo.setEmpNumber(list.get(i).getEmpNum());
            vo.setAward(list.get(i).getOvertimeSalary()+list.get(i).getMonthBonus()+list.get(i).getHouseSalary()+list.get(i).getFoodSalary()+list.get(i).getTravelSalary());
            vo.setTakeOut(list.get(i).getTax()+list.get(i).getIllegalFine()+list.get(i).getLeaveFine());

            salaryVOList.add(vo);
        }
        MyPageInfo<SalaryToPageAdmin> myPageInfo = new MyPageInfo<>();
        myPageInfo.setList(salaryVOList);
        myPageInfo.setPageNum(pageNum);
        myPageInfo.setPageSize(pageSize);
        myPageInfo.setRecordCount((int) pageInfo.getTotal());
        myPageInfo.setTotalPage(pageInfo.getPages());
        return Result.success().add("list",myPageInfo);
    }


    /*初始化日期*/
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
    }
}
